import Taro from "@tarojs/taro"

export const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 格式化时间
 * @param date 日期
 * @returns 
 */
export const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

/**
 * 日志信息
 * @param name 
 * @param action 出错的action
 * @param info 错误详细信息
 */
export const logError = (name, action, info = 'empty') => {
  let device: string = "";
  try {
    let deviceInfo = Taro.getSystemInfoSync()
    device = JSON.stringify(deviceInfo)
  } catch (err) {
    console.error('not support getSystemInfoSync api', err.message)
  }
  let time = formatTime(new Date())
  console.error(time, name, action, info, device)
  // if (typeof action !== 'object') {
  // fundebug.notify(name, action, info)
  // }
  // fundebug.notifyError(info, { name, action, device, time })
  if (typeof info === 'object') {
    info = JSON.stringify(info)
  }
}

export const getTerminalType = (): String => {
  let clientType = "";
  switch (Taro.getEnv()) {
    case "WEAPP":
      clientType = "WX_MP";
      break;
    case "ALIPAY":
      clientType = "ALIAPY_MP";
      break;
    case "WEB":
      clientType = "H5";
      break;
    default:
  }
  return clientType;
}